package com.yonyou.pmclouds.organization.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.outter.role.UserRoleInfoQuery;
import com.yonyou.pmclouds.outter.tenant.TenantInfoQuery;
import com.yonyou.pmclouds.tenant.entity.TenantInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping({"/organization", "/mobile/organization"})
@RestController
@Slf4j
public class OrgRefController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private OrganizationQuery organizationQuery;
    @Autowired
    private TenantInfoQuery tenantInfoQuery;
    @Autowired
    private UserRoleInfoQuery userRoleInfoQuery;

    /**
     * 获得租户下全部组织
     * @param tenantId
     * @return
     * @throws BusinessException
     * @throws IOException
     */
    @RequestMapping("/getOrgByTenantId")
    public OrganizationVO getOrgListByTenantId(@RequestParam("tenantId") String tenantId) throws BusinessException, IOException {
        OrganizationVO[] childVOs = organizationQuery.queryOrgListByTenantId(tenantId);
        OrganizationVO root = getOrgTenantVO(tenantId);
        root.setChildren(childVOs);
        return root;
    }

    /**
     * 获得用户有权限的组织
     * @param pkTenant
     * @return
     * @throws BusinessException
     * @throws IOException
     */
    @RequestMapping("/getPermOrgByUser")
    public OrganizationVO getPermOrgByUser(@RequestParam(name = "pkTenant", required = false) String pkTenant) throws BusinessException, IOException {

        if (StringUtils.isEmpty(pkTenant)) {
            pkTenant = RuntimeEnvironment.getTenantId();
        }
        String pkUser = RuntimeEnvironment.getLoginUserId();
        String[] pkRoles = userRoleInfoQuery.getUserRoles(pkUser, pkTenant);

        OrganizationVO[] childVOs = new OrganizationVO[0];
        if (ArrayUtils.isNotEmpty(pkRoles)) {
            childVOs = organizationQuery.queryOrgByRoles(pkTenant, pkRoles);
        }

        OrganizationVO root = getOrgTenantVO(pkTenant);
        root.setChildren(childVOs);
        return root;
    }

    /**
     * 获得租户信息并封装成组织VO
     * @param tenantId
     * @return
     * @throws BusinessException
     */
    private OrganizationVO getOrgTenantVO(String tenantId) throws BusinessException {
        TenantInfo tenantInfo = tenantInfoQuery.queryTenantInfoById(tenantId);

        OrganizationVO root = new OrganizationVO();
        root.setPkTenant(tenantId);
        if (tenantInfo != null ) {
            root.setTenantName(tenantInfo.getTenantName());
            root.setTenantCode(tenantInfo.getTenantCode());
            root.setSname(tenantInfo.getTenantName());  //为了前台方便
            root.setScode(tenantInfo.getTenantCode());
        }
        return root;
    }

}
